package xin.mashibo.genvictobutest.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.widget.ArrayAdapter;
import android.widget.ListView;

import com.genvict.obusdk.data.DeviceInformation;

import java.util.ArrayList;
import java.util.List;

import xin.mashibo.genvictobutest.R;

import static xin.mashibo.genvictobutest.constant.ServiceConstant.DEVICE_INFO_KEY;

public class DeviceInfoActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.listview);
        setTitle(R.string.deviceActivityTitle);
        Bundle bundle = getIntent().getBundleExtra(DEVICE_INFO_KEY);
        DeviceInformation deviceInfo = bundle.getParcelable(DEVICE_INFO_KEY);
        ListView listView = findViewById(R.id.list_view);
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, generateListData(deviceInfo));
        listView.setAdapter(adapter);
    }

    /**
     * 启动设备信息活动
     *
     * @param context 启动的活动
     * @param bundle  bundle
     */
    public static void actionStart(Context context, Bundle bundle) {
        Intent intent = new Intent(context, DeviceInfoActivity.class);
        intent.putExtra(DEVICE_INFO_KEY, bundle);
        context.startActivity(intent);
    }

    /**
     * 构建设备信息列表数据
     *
     * @return 设备信息列表
     */
    public List<String> generateListData(DeviceInformation deviceInfo) {
        List<String> dataList = new ArrayList<>();
        dataList.add("设备名称：" + (TextUtils.isEmpty(deviceInfo.DevName) ? "" : deviceInfo.DevName));
        dataList.add("设备地址：" + (TextUtils.isEmpty(deviceInfo.DevAddress) ? "" : deviceInfo.DevAddress));
        dataList.add("版本ID：" + (TextUtils.isEmpty(deviceInfo.VerId) ? "" : deviceInfo.VerId));
        dataList.add("版本号：" + (TextUtils.isEmpty(deviceInfo.Version) ? "" : deviceInfo.Version));
        dataList.add("电量：" + (TextUtils.isEmpty(deviceInfo.Battery) ? "" : deviceInfo.Battery));
        dataList.add("序列号：" + (TextUtils.isEmpty(deviceInfo.Sn) ? "" : deviceInfo.Sn));
        return dataList;
    }
}
